package sparrowGui
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import sparrowGui.impl.IScrollPan;
	import sparrowGui.skin.HScrollBarSkin;
	import sparrowGui.utils.PubFun;
	
	/**
	 * 有横纵滚动条的列表
	 * 将一维数组数据按表格排列，每行X列，超过X列的自动换行到下一行排
	 * @author pelephone
	 */	
	public class VHList extends VList implements IScrollPan
	{
		protected var _hScroll:HScrollBar;
		
		public function VHList(data:Object=null,itemSkinCla:Class=null,vars:Object=null)
		{
			if(!vars) vars = SparrowGUI.getIns().vhlist;
			var ssk:Sprite = vars.hScrollSkin || new HScrollBarSkin();
			_hScroll = new HScrollBar(ssk);
			super(data,itemSkinCla,vars);
		}
		
		override protected function init() : void
		{
			super.init();
			addChild(_hScroll);
		}
		
		override protected function activeScroll(step:Number=0,disHeight:Number=0):void
		{
			disHeight = disHeight || (((contDP.width-maskDP.width)>0)?VScrollBar.THICKNESS:0);
			super.activeScroll(step,disHeight);
			
			activeHBar();
		}
		
		protected function activeHBar(disHeight:Number=0):void
		{
			_hScroll.height = VScrollBar.THICKNESS;
			_hScroll.width	 = style.width - disHeight;
			
			// 大于0的时候已经遮过了,故不用再遮
			if((contDP.height - maskDP.height)<=0){
				PubFun.alignRect(maskDP,contDP,"TL");
				contDP.mask = maskDP;
			}
			maskDP.scaleY = 1;
			_hScroll.setSliderParams(maskDP.width,contDP.width);
			// 未滚动条未激活状态,autoHide为0,或者2则将其隐藏
			if(!_hScroll.enabled && !style.autoHide){
				_hScroll.visible = false;
				return;
			}
			maskDP.height = style.height - _hScroll.height;
			// 激活滚动条后才知道其是否显示,而后设置位置
			_hScroll.x = 0;
			_hScroll.y = style.height - _hScroll.height;
		}
		
		override protected function addEvt():void
		{
			super.addEvt();
			if(scrollWidth>0){
				if(_hScroll.hasEventListener(Event.CHANGE)) return;
				_hScroll.addEventListener(Event.CHANGE,onHScroll);
			}else
				delEvt();
		}
		
		override protected function delEvt():void
		{
			if(_hScroll) _hScroll.removeEventListener(Event.CHANGE,onScroll);
//			removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		override protected function arrangeItems():void
		{
			PubFun.arrangeDispRC(contDP,style.colNum,style.colWidth,style.rowHeight,style.spacing);
		}
		
		protected function onHScroll(event:Event):void
		{
			if(!_hScroll) return;
			contDP.x = -_hScroll.value * scrollWidth + maskDP.x;
		}
		
		private function get scrollWidth():Number
		{
			return (contDP.width - maskDP.width);
		}
		
		/**
		 * 覆盖宽属性
		 */
		override public function set width(w:Number):void
		{
			_hScroll.width = w;
			super.width = w;
		}
		
/*		override public function set space(val:int):void
		{
			style.space = val;
		}*/
	}
}